<template>
    <div class="mainDiv">
        <el-row :gutter="20" class="rowHead">
            <el-col :span="12" class="flex_center">
                <el-button icon="el-icon-plus" type="success" class="mr" @click="adduser" v-qx="quanxian.add" style="margin-right: 5px;">新增员工</el-button>
                <el-select v-model="page.zt" @change="getList" v-qx="quanxian.status">
                    <el-option v-for="(o,index) in leaves" :label="o.txt" :value="o.val" :key="index">
                    </el-option>
                </el-select>
            </el-col>
            <el-col :span="12" class="flex_center">
                <el-input placeholder="搜索员工姓名、工号、电话" v-model="page.search" @keyup.enter.native="getList">
                    <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="getList"></i>
                </el-input>                
                <el-button type="primary" icon="el-icon-h-shuaxin1" style="margin-left: 5px;" @click="refresh">刷新列表</el-button>
            </el-col>           
        </el-row>
        <el-table :data="list" :stripe="true" height="100%">
            <el-table-column type="index" label="#" :index="uIndex"></el-table-column>
            <el-table-column prop="name" label="员工姓名"></el-table-column>
            <el-table-column prop="worknum" label="员工工号"></el-table-column>
            <el-table-column prop="sex" label="性别">
                <template slot-scope="scope">
                    <i class="el-icon-h-nan1 color-nan" v-if="scope.row.sex" size="mini"></i>
                    <i class="el-icon-h-nv1 color-nv" v-else size="mini"></i>
                </template>
            </el-table-column>
            <el-table-column prop="username" label="员工电话"></el-table-column>
            <el-table-column prop="departmentname" label="所在部门"></el-table-column>
            <el-table-column prop="positionname" label="所属职位"></el-table-column>
            <el-table-column prop="zhichengname" label="职位职称"></el-table-column>
            <el-table-column prop="ismanage" label="管理层">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.ismanage" size="mini">
                        <i class="el-icon-h-shi"></i>
                    </el-tag>
                    <el-tag type="danger" v-else size="mini">
                        <i class="el-icon-h-fou"></i>
                    </el-tag>
                </template>
            </el-table-column>
            <!-- <el-table-column prop="remarks" label="描述" show-overflow-tooltip></el-table-column> -->
            <el-table-column prop="addtime" label="录入日期" show-overflow-tooltip>
                <template slot-scope="scope">
                    {{scope.row.addtime|dateTime}}
                </template>
            </el-table-column>
            <el-table-column label="信息操作" width="120">
                <template slot-scope="scope">
                    <el-dropdown trigger="click" @command="moreMenu" v-if="!scope.row.isdisable">
                        <el-button type="primary" size="mini">更多操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item :command="{row: scope.row, tp: 1}" v-qx="quanxian.edit">编辑员工</el-dropdown-item>
                            <el-dropdown-item :command="{id: scope.row.id, tp: 2}" v-qx="quanxian.reset">重置密码</el-dropdown-item>
                            <el-dropdown-item :command="{id: scope.row.id, tp: 3}" v-qx="quanxian.unstop">员工离职</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                    <el-tooltip content="复职" placement="top" :open-delay="800" v-else v-qx="quanxian.unstop">
                        <el-button icon="el-icon-h-qiyong2" type="success" size="mini" @click="fuzhi(scope.row)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div style="padding:4px 0 0 0">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="page.pages" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'
import User from '@/service/shop/user.js'
export default {
  name: 'userList',
  data () {
    return {
      list: [],
      total: 0,
      page: {
        pages: 1,
        limits: 15,
        zt: 0,
        search: ''
      },
      leaves: [{
        txt: '在职员工',
        val: 0
      }, {
        txt: '离职员工',
        val: 1
      }],
      quanxian: {
        add: 'worker_add',
        edit: 'worker_edit',
        unstop: 'worker_unstop',
        status: 'worker_status',
        reset: 'worker_reset'
      }
    }
  },
  methods: {
    moreMenu: function (com) {
      if (com.tp === 1) {
        this.edit(com.row)
      } else if (com.tp === 2) {
        this.resetPassWord(com.id)
      } else if (com.tp === 3) {
        this.shanchu(com.id)
      }
    },

    async resetPassWord (id) {
      C.confirm('重置当前员工帐号密码为当前手机号，是否继续？', async () => {
        let res = await C.ajax('/shop/user/resetpassword', { id: id })
        if (res) {
          C.success(res.data)
        }
      })
    },
    refresh: function () {
      this.getList()
    },
    async adduser () {
      let res = await User.openAddUser()
      console.log(res)
      if (res) {
        this.getList()
        C.success(res.data)
      }
    },
    async edit (row) {
      let res = await User.openAddUser(row)
      if (res) {
        this.getList()
        C.success(res.data)
      }
    },
    // 删除订单
    async shanchu (id) {
      C.confirm('员工离职后将不会在显示当前员工信息，并且解除当前员工绑定顾客。是否继续？', async () => {
        let res = await C.ajax('/shop/user/unstop', { id: id }, 'post')
        if (res) {
          C.success(res.data)
          this.getList()
        }
      })
    },

    sizeChange: function (val) {
      this.page.limits = val
      this.getList()
    },
    currentChange: function (val) {
      this.page.pages = val
      this.getList()
    },

    uIndex (index) {
      return (this.page.pages - 1) * this.page.limits + index + 1
    },

    async getList () {
      let data = await C.ajax('/shop/user/userlist', { page: JSON.stringify(this.page) }, 'post')
      if (data) {
        this.list = data.data
        this.total = data.count
      }
    },
    fuzhi: async function (row) {
      let res = await User.openAddUser(row)
      if (res) {
        this.getList()
        C.success(res.data)
      }
    }
  },
  mounted: function () {
    this.getList()
  }
}
</script>